package cn.com.huzo.repository.master.impl;

import cn.com.huzo.model.entities.master.CustomerEntity;
import huzofw.repository.impl.GenericRepositoryImpl;
import huzofw.repository.impl.MyBatisDaoUtils;
import cn.com.huzo.repository.master.CustomerRepository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;

/**
 * 客户信息数据访问对象。
 *
 * @author 吴旭
 */
@Repository("CustomerRepository")
public class CustomerRepositoryImpl extends GenericRepositoryImpl<CustomerEntity, Long>
        implements CustomerRepository {

    private final String namespace = "cn.com.huzo.repository.master.CustomerRepository";

    /**
     * 构造函数,设置the instance to CustomerRepositoryImpl.class.
     */
    public CustomerRepositoryImpl() {
        super(CustomerEntity.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public CustomerEntity fetchByKey(String rootNodeName, Long key) {
        Map<String, Object> params = new HashMap<>();
        params.put("RootNodeName", rootNodeName);
        params.put("CustomerId", key);
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchByKey"), params);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public CustomerEntity fetchByCustomerNo(String rootNodeName, String customerNo) {
        Map<String, Object> params = new HashMap<>();
        params.put("RootNodeName", rootNodeName);
        params.put("CustomerNo", customerNo);

        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchByCustomerNo"), params);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer fetchForCount(Map<String, Object> criterion) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchForCount"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<CustomerEntity> fetchForPage(Map<String, Object> criterion) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchForPage"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long insert(CustomerEntity instance) {
        getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insert"), instance);

        // 返回出入后的记录ID
        return instance.getCustomerId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer update(CustomerEntity instance) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "update"), instance);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer delete(Long key) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "delete"), key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<CustomerEntity> fetchAllFields() {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchAllFields"));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<String> fetchExportData(Map<String, Object> criterion) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchExportData"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer batchChangeCatalogue(List<CustomerEntity> itemList) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "batchChangeCatalogue"), itemList);
    }
}
